/*
	Flie:						OptionsState.cpp
	Course:				SGP 1208
	Author:				Kori Po
	Purpose:			Options Menu
*/

#include "Game.h"
#include "MainMenuState.h"
#include "OptionsState.h"

// Temporary Development Includes
#include "TempFont.h"
#include "../TinyXML/tinyxml.h"


COptionsState::COptionsState(void)
{
	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;

	m_nBackGroundID = -1;
	m_nOptionsBoxID = -1;

	m_nSelectedItem = 0;

	CGame* TheGame = CGame::GetInstance();

	for(int x = 0; x < OPTIONSMENUITEMS; x++)
	{
		m_MenuSelection[x] = false;

		RECT r = {TheGame->GetScreenWidth()/4,
						(TheGame->GetScreenHeight()/3) + (75 * x), 
						TheGame->GetScreenWidth()/4 + (225*2),
						(TheGame->GetScreenHeight()/3) + (75 * x) + 43 };

		m_MenuRect[x] = r;
	}

	m_szMenuItem[0] = "TOGGLE\nFULLSCREEN: ";
	m_szMenuItem[1] = "MUSIC VOLUME: ";
	m_szMenuItem[2] = "SFX VOLUME: ";
	m_szMenuItem[3] = "GO BACK";
}

COptionsState::~COptionsState(void)
{
	/* Do Nothing */
}

/*static*/ COptionsState* COptionsState::GetInstance( void )
{
	// Static local variable - Stored in GLOBAL memory
	static COptionsState s_instance;
	return &s_instance;
}

void COptionsState::Enter(void)
{
	m_3DGuy					= CSGD_Direct3D::GetInstance();
	m_InputGuy			= CSGD_DirectInput::GetInstance();
	m_TextureGuy		= CSGD_TextureManager::GetInstance();
	m_AudioGuy			= CSGD_XAudio2::GetInstance();

	m_nMenuWizardID	= m_TextureGuy->LoadTexture(_T("Resource/TempMouseCursor.png"), D3DCOLOR_XRGB(255,255,255));
	m_nBackGroundID	= m_TextureGuy->LoadTexture(_T("Resource/TempFusionBackground.png"), D3DCOLOR_XRGB(255,255,255));
	m_nOptionsBoxID		= m_TextureGuy->LoadTexture(_T("Resource/TempOptionItemBox.png"), D3DCOLOR_XRGB(255,255,255));

	m_nMusicID					= m_AudioGuy->MusicLoadSong(_T("Resource/TempBGM.xwm"));
	m_nSFXID						= m_AudioGuy->SFXLoadSound(_T("Resource/TempSFX.wav"));

	m_MenuFont				= new CTempFont( );

	LoadOptions();

	m_AudioGuy->MusicPlaySong(m_nMusicID,true);
	m_AudioGuy->SFXPlaySound(m_nSFXID,true);

	//m_nMasterMusicVolume		= 100 * m_AudioGuy->MusicGetMasterVolume();
	//m_nMasterSFXVolume			= 100 * m_AudioGuy->SFXGetMasterVolume();
}

void COptionsState::Exit(void)
{
	SaveOptions();

	m_AudioGuy->MusicStopSong(m_nMusicID);
	m_AudioGuy->SFXStopSound(m_nSFXID);

	if(m_nMenuWizardID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nMenuWizardID);
		m_nMenuWizardID = -1;
	}
	if(m_nOptionsBoxID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nOptionsBoxID);
		m_nOptionsBoxID = -1;
	}

	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;
}

bool COptionsState::Input(void)
{
	if( m_InputGuy->KeyPressed( DIK_ESCAPE )  || m_InputGuy->JoystickButtonPressed( 2, 0))
	{
		CGame* TheGame	=	CGame::GetInstance();
		CMainMenuState* TheMenu = CMainMenuState::GetInstance();
		TheGame->ChangeState(TheMenu);
		return true;
	}

	if( m_InputGuy->KeyDown( DIK_LMENU ) && m_InputGuy->KeyPressed( DIK_RETURN )|| m_InputGuy->KeyDown( DIK_RMENU ) && m_InputGuy->KeyPressed( DIK_RETURN ) )
	{
			CGame* TheGame	=	CGame::GetInstance();
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
			return true;
	}

	if( m_InputGuy->KeyPressedEx(DIK_DOWN) || m_InputGuy->JoystickDPadPressed(DIR_DOWN, 0) )
	{
		if(m_nSelectedItem + 1 > 3)
		{
			m_nSelectedItem = 0;
		}
		else
		{
			m_nSelectedItem++;
		}
	}
	if(  m_InputGuy->KeyPressedEx(DIK_UP) || m_InputGuy->JoystickDPadPressed(DIR_UP, 0) )
	{
		if(m_nSelectedItem - 1 < 0)
		{
			m_nSelectedItem = 3;
		}
		else
		{
			m_nSelectedItem--;
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_LEFT) || m_InputGuy->JoystickDPadPressed(DIR_LEFT, 0) )
	{
		if(m_nSelectedItem == 1)
		{
			if(!(m_nMasterMusicVolume <= 0))
			{
				m_nMasterMusicVolume -= 5.0f;
				float temp = float(m_nMasterMusicVolume*.01);
				m_AudioGuy->MusicSetMasterVolume(temp);
			}
		}
		else if(m_nSelectedItem == 2)
		{
			if(!(m_nMasterSFXVolume <= 0))
			{
				m_nMasterSFXVolume -= 5.0f;
				float temp = float(m_nMasterSFXVolume*.01);
				m_AudioGuy->SFXSetMasterVolume(temp);					
			}
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_RIGHT) || m_InputGuy->JoystickDPadPressed(DIR_RIGHT, 0))
	{
		if(m_nSelectedItem == 1)
		{
			if(!(m_nMasterMusicVolume >= 100))
			{
				m_nMasterMusicVolume += 5.0f;
				float temp = float(m_nMasterMusicVolume*.01);
				m_AudioGuy->MusicSetMasterVolume(temp);
			}
		}
		else if(m_nSelectedItem == 2)
		{
			if(!(m_nMasterSFXVolume >= 100))
			{
				m_nMasterSFXVolume += 5.0f;
				float temp = float(m_nMasterSFXVolume*.01);
				m_AudioGuy->SFXSetMasterVolume(temp);					
			}
		}
	}

	CGame* TheGame = CGame::GetInstance();

	if( m_InputGuy->KeyPressedEx(DIK_RETURN ) || m_InputGuy->MouseButtonPressed(0) || m_InputGuy->JoystickButtonPressed( 0, 0))
	{
		if(m_nSelectedItem == 0)
		{
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
			return true;
		}
		else if(m_nSelectedItem == 1)
		{
			return true;
		}
		else if(m_nSelectedItem == 2)
		{
			return true;
		}
		else if(m_nSelectedItem == 3)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CMainMenuState* TheMain = CMainMenuState::GetInstance();
			TheGame->ChangeState(TheMain);
			return true;
		}
	}

	for(int x = 0; x < OPTIONSMENUITEMS; x++)
	{
		m_MenuSelection[x] = false;
	}
	m_MenuSelection[m_nSelectedItem] = true;
	
	return true;
}

void COptionsState::Update(float fElapsedTime)
{
	Mouseinthebox( );
}

void COptionsState::Render(void) 
{
	CGame* TheGame = CGame::GetInstance();

	//Draw Background
	RECT ToDraw = {0,0,1920,1080};
	m_TextureGuy->Draw(m_nBackGroundID,0,0,1,1,&ToDraw);

	// Draw Menu
	for(int x = 0; x < OPTIONSMENUITEMS; x++)
	{
		m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth()/4,(TheGame->GetScreenHeight()/3) + (75 * x),2,1);
	
		TCHAR  	bufferA[ 100 ];
		TCHAR  	bufferB[ 100 ];
		
		char	strMusic[100];
		char	strSFX[100];

		int VolMusic = m_nMasterMusicVolume;
		int VolSFX = m_nMasterSFXVolume;
		
		_stprintf_s( bufferA, 100, _T("MUSIC VOLUME: %i"), VolMusic );
		_stprintf_s( bufferB, 100, _T("SFX VOLUME: %i"), VolSFX );

		wcstombs_s( nullptr, strMusic, 100, bufferA, _TRUNCATE );
		wcstombs_s( nullptr, strSFX, 100, bufferB, _TRUNCATE );

		if(!m_MenuSelection[x])
		{
			if(x == 0)
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1, D3DCOLOR_XRGB(255,255,255));
				if(TheGame->GetIsWindowed())
				{
					m_MenuFont->Print("OFF", TheGame->GetScreenWidth()/4 + 400,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1, D3DCOLOR_XRGB(255,255,255));
				}
				else
				{
					m_MenuFont->Print("ON", TheGame->GetScreenWidth()/4 + 400,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1, D3DCOLOR_XRGB(255,255,255));
				}
			}
			else if(x == 1)
			{
				m_MenuFont->Print(strMusic, TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1, D3DCOLOR_XRGB(255,0,255));
			}
			else if( x == 2)
			{
				m_MenuFont->Print(strSFX, TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1, D3DCOLOR_XRGB(255,0,255));
			}
			else
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1, D3DCOLOR_XRGB(255,0,255));
			}
		}
		else
		{
			if(x == 0)
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1.4, D3DCOLOR_XRGB(255,255,255));
				if(TheGame->GetIsWindowed())
				{
					m_MenuFont->Print("OFF", TheGame->GetScreenWidth()/4 + 400,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1.4, D3DCOLOR_XRGB(255,255,255));
				}
				else
				{
					m_MenuFont->Print("ON", TheGame->GetScreenWidth()/4 + 400,(TheGame->GetScreenHeight()/3) + (75 * x) + 5, 1.4, D3DCOLOR_XRGB(255,255,255));
				}
			}
			else if(x == 1)
			{
				m_MenuFont->Print(strMusic, TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1.4, D3DCOLOR_XRGB(255,0,255));
			}
			else if( x == 2)
			{
				m_MenuFont->Print(strSFX, TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1.4, D3DCOLOR_XRGB(255,0,255));
			}
			else
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/4 + 15,(TheGame->GetScreenHeight()/3) + (75 * x) + 15, 1.4, D3DCOLOR_XRGB(255,0,255));
			}
		}
	}

	// Draw Title
	m_MenuFont->Print("OPTIONS", TheGame->GetScreenWidth()/2 - 50, 20, 1, D3DCOLOR_XRGB(255,255,255));

	// Draw Mouse
	RECT ToDrawMouse = {0,0,38,48};
	if(m_InputGuy->MouseButtonDown(MOUSE_LEFT))
	{
		RECT ToDrawMouse = {39,0,75,48};
	}

	m_TextureGuy->Draw(m_nMenuWizardID,m_InputGuy->MouseGetPosX(),m_InputGuy->MouseGetPosY(),1,1,&ToDrawMouse);
}

void COptionsState::Mouseinthebox()
{
	int MouseX = m_InputGuy->MouseGetPosX();
	int MouseY = m_InputGuy->MouseGetPosY();
	
	POINT Clicker;
	Clicker.x = m_InputGuy->MouseGetPosX();
	Clicker.y = m_InputGuy->MouseGetPosY();

	CGame* TheGame = CGame::GetInstance();
	
	for(int x = 0; x < MAINMENUITEMS; x++)
	{
		if(PtInRect(&m_MenuRect[x], Clicker))
		{
			m_nSelectedItem = x;
		}
	}
}

void COptionsState::SaveOptions()
{
	TiXmlDocument TheDoc;

	TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "utf-8", " ");

	TheDoc.LinkEndChild(pDecl);

	// Root Node
	TiXmlElement* pRoot = new TiXmlElement("Options");

	TheDoc.LinkEndChild(pRoot);

	// Node
	TiXmlElement* pVolume = new TiXmlElement("Volumes");

	pVolume->SetAttribute("Music", this->m_nMasterMusicVolume);
	pVolume->SetAttribute("SFX", this->m_nMasterSFXVolume);

	pRoot->LinkEndChild(pVolume);

	// Node
	TiXmlElement* pFullScreen = new TiXmlElement("Fullscreen");

	CGame* TheGame	=	CGame::GetInstance();
	
	pFullScreen->SetAttribute("FullScreen", (!TheGame->GetIsWindowed()));
	
	pRoot->LinkEndChild(pFullScreen);

	//Save the File
	TheDoc.SaveFile("Resource/Files/OptionsFile.xml");
}

bool COptionsState::LoadOptions()
{
	CGame* TheGame	=	CGame::GetInstance();

	TiXmlDocument TheDoc;

	if(TheDoc.LoadFile("Resource/Files/OptionsFile.xml"))
	{
		TiXmlElement* pRoot = TheDoc.RootElement();

		if(pRoot == nullptr)
			return false;

		TiXmlElement* pVolume = pRoot->FirstChildElement("Volumes");

		int nTemp;

		pVolume->Attribute("Music", &m_nMasterMusicVolume);
		pVolume->Attribute("SFX", &m_nMasterSFXVolume);

		m_AudioGuy->MusicSetMasterVolume((m_nMasterMusicVolume*.01));		
		m_AudioGuy->SFXSetMasterVolume((m_nMasterSFXVolume*.01));

		TiXmlElement* pFullScreen = pRoot->FirstChildElement("Fullscreen");

		pFullScreen->Attribute("FullScreen", &nTemp);
		if(nTemp)
		{
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
		}
	}
	else
	{
		m_nMasterMusicVolume		= 100;
		m_nMasterSFXVolume			= 100;
	}
	return true;
}